import Vue from 'vue'
import Vuex from 'vuex'
import { deepCopy, delIdData } from '@/function'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        loginUser : null,            //当前登录用户信息
        selectedComponent: null,     //一级组件
        selectedSubComponent : null, //二级组件
        sqId: null,                  //当前页面序列id
        pageHtmlElements: [],        //当前页面所有元素集合
        loginModalVisible : false,   //登录态模态框
        componentActiveTab : null,   //配置界面当前tag
        pageGlobalConfig : {         //页面全局配置
            pageTitle : '妙笔移动建站平台'
        }
    },
    mutations: {
        //显示登录模态框
        showLoginModal(state, visible){
            state.loginModalVisible = visible
        },
        //保存当前用户信息
        setLoginUser(state, obj){
            state.loginUser = obj
        },
        //设置当前页面的序列id
        setSqId(state, id){
            state.sqId = id;
        },
        //选中当前选中的二级组件
        setSelectedSubComponent(state, component){
            if (state.selectedComponent === null){
                return
            }
            state.selectedSubComponent = component
        },
        //选中当前选中的一级组件
        setSelectedComponent(state, item) {
            state.selectedComponent = item
        },
        //设置当前页面元素数据
        setPageHtmlElements (state, elements) {
            state.pageHtmlElements = elements
        },
        //设置当前页面全局配置信息
        setPageGlobalConfig (state, config) {
            state.pageGlobalConfig = config
        },
        //根据索引删除页面元素
        removePageHtmlElement(state, index) {
            //如果当前配置区域控件为选择的空间, 那么重置为空
            if(state.selectedComponent !== null && state.pageHtmlElements[index].data.id === state.selectedComponent.data.id) {
                state.selectedComponent = null
            }
            state.pageHtmlElements.splice(index, 1)
        },
        //根据索引拷贝一个元素信息
        copyPageHtmlElement(state, index) {
            let component = state.pageHtmlElements[index];
            let cp = deepCopy(component)
            cp.data.id = Date.now();
            state.pageHtmlElements.splice(index, 0, cp)
        },
        //根据id删除当前页面的元素
        delElement(state, id ){
            state.pageHtmlElements = delIdData(state.pageHtmlElements, id)
            state.selectedComponent = null
        },
        //设置当前活动的组件配置tab
        setComponentActiveTab(state, index){
            state.componentActiveTab = index
        }
    },
    actions: {},
    modules: {}
})
